#!/bin/bash
#
# 29/09/2006 - Vincenzo Tozzi - vincenzo.tozzi@planalto.gov.br - ramal 3334
# 05/10/2007 - Ricardo Brito - ricardo.brito[at]planalto.gov.br - ramal 2783
#
# Pode ser esecutado automaticamente fazendo 'crontab -e' e colocando (para rodar todos os dias as 3.30 da manha):
# SHELL=/bin/bash
# # Backup Data.fs do Zope
# 30 3 * * * /home/zope/bin/backup
#
echo proxy loading...
export http_proxy=http://10.1.101.101:8080

#export PYTHONPATH=/home/zope/zope2.9/lib/python
#PYTHON=/home/zope/python2.4/bin/python
#ZOPEUTIL=/home/zope/zope2.9/bin
#BCK_DIR=/home/zope/backups
#ZEO_DIR=/home/zope/instancias/zeo


echo variables loading...
export PYTHONPATH=/opt/Plone-2.5.3/Python-2.4.4/lib/python2.4
PYTHON=/opt/Plone-2.5.3/Python-2.4.4/bin/python
ZOPEUTIL=/opt/Plone-2.5.3/bin
BCK_DIR=/opt/Plone-2.5.3/backups
ZEO_DIR=/opt/Plone-2.5.3/intracluster/zeo
ZEO_PORT=8200
SERVERNAME=ipanema

echo Creating log file...
echo ":: Relatorio de backup do Data.fs ($SERVERNAME) ::" > $BCK_DIR/message.txt

echo Appending date in log file...
date >> $BCK_DIR/message.txt

echo locating and move olds backups files...
test -e $BCK_DIR/*.dat && mv $BCK_DIR/*.dat $BCK_DIR/antigo/
test -e $BCK_DIR/*.fs  && mv $BCK_DIR/*.fs  $BCK_DIR/antigo/

echo Doing the pack ZEO...
echo "Fazendo o pack do ZEO ..."  >> $BCK_DIR/message.txt
$PYTHON $ZOPEUTIL/zeopack.py -p $ZEO_PORT -d 7 2> $BCK_DIR/tempfile
cat $BCK_DIR/tempfile >> $BCK_DIR/message.txt

echo Doing backup Data.fs ...
echo "Fazendo backup incremental do Data.fs ..." >> $BCK_DIR/message.txt
$PYTHON $ZOPEUTIL/repozo.py -B -v -r $BCK_DIR -f $ZEO_DIR/var/Data.fs 2> $BCK_DIR/tempfile

cat $BCK_DIR/tempfile >> $BCK_DIR/message.txt

# Copia em outro servidor
echo "Copiando o backup no servidor (grande 10.1.2.150)..." 
echo "Copiando o backup no servidor (grande 10.1.2.150)..." >> $BCK_DIR/message.txt
scp -rv $BCK_DIR/*.fs iaco@10.1.2.150:backup/ 2> $BCK_DIR/tempfile
scp -rv $BCK_DIR/*.dat iaco@10.1.2.150:backup/ 2> $BCK_DIR/tempfile
cat $BCK_DIR/tempfile >> $BCK_DIR/message.txt

date >> $BCK_DIR/message.txt

cat $BCK_DIR/message.txt | mailx -s 'Cron Job: backup PZP' root

echo finish log file ...
rm $BCK_DIR/message.txt $BCK_DIR/tempfile
